package org.firecloud.h5fun.model;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import javax.servlet.http.HttpServletRequest;
/**
 * 通用函数类<br>
 * @author fc
 */
public class CommonFunction {
	
	/**
	 * 发送邮箱验证码函数
	 * @param target 目标邮箱地址
	 * @param title 邮件标题
	 * @param content 邮件内容
	 * @throws AddressException 邮箱地址错误
	 * @throws MessagingException 消息错误
	 */
	public final static void send_mail(String target, String title, String content)
			throws AddressException, MessagingException {
		MailConfig a = MailConfig.get_mail_config();
		Properties prop = MailConfig.get_mail_config().prop;
		Session s = Session.getInstance(prop, a);
		MimeMessage message = new MimeMessage(s);
		message.setFrom(new InternetAddress(a.username));
		message.setRecipient(RecipientType.TO, new InternetAddress(target));
		message.setSubject(title);
		message.setContent(content.toString(), "text/html;charset=utf-8");
		Transport.send(message);
	}
	
	/**
	 * 检查req中参数与session中属性是否一致
	 * @param req 请求
	 * @param codename 参数和属性的名字
	 * @return 相同返回True不同返回False
	 */
	public final static boolean check_vcode(HttpServletRequest req,String codename){
		return req.getParameter(codename) != null && req.getParameter(codename).equals(req.getSession().getAttribute(codename));
	}
	
	/**
	 * 检查邮箱格式是否正确
	 * @param mail 要检查的邮箱
	 * @return 正确返回True错误返回False
	 */
	public final static boolean check_mail(String mail){
		if(mail==null){
			return false;
		}
		if(mail.length()>32){
			return false;
		}
		if(mail.indexOf('@',1)==-1){
			return false;
		}
		return true;
	}
	
	/**
	 * 检查用户名格式是否正确
	 * @param name 要检查的用户名
	 * @return 正确返回True错误返回False
	 */
	public final static boolean check_name(String name){
		if(name==null){
			return false;
		}
		if(name.length()>20){
			return false;
		}
		return true;
	}
	
	/**
	 * 检查密码格式是否正确
	 * @param password 要检查的密码
	 * @return 正确返回True错误返回False
	 */
	public final static boolean check_password(String password){
		if(password==null){
			return false;
		}
		if(password.length()<6){
			return false;
		}
		if(password.length()>32){
			return false;
		}
		return true;
	}
	
	/**
	 * 读取文件内容为String
	 * @param filename 要读取的文件名
	 * @return 文件内容String
	 * @throws FileNotFoundException 文件不存在
	 * @throws IOException 文件读取错误
	 */
	public final static String file_read_string(String filename) throws FileNotFoundException,IOException{
		FileInputStream fis = new FileInputStream(filename);	
		BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
		StringBuffer sb = new StringBuffer();
		String line = null;
		while((line=bufferReader.readLine())!=null){
			sb.append(line);
		}
		bufferReader.close();
		return sb.toString();
	}
	
	/**
	 * 发起HttpGet请求，并返回数据
	 * @param url 要请求的URL
	 * @return 返回的数据
	 * @throws IOException 网络错误
	 */
	public final static String http_read_get(String url) throws IOException{
		URL realUrl = new URL(url);
		URLConnection connection = realUrl.openConnection();
		connection.connect();
		BufferedReader bufferReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		StringBuffer sb = new StringBuffer();
		String line = null;
		while((line=bufferReader.readLine())!=null){
			sb.append(line);
		}
		bufferReader.close();
		return sb.toString();
	}
	
	/**
	 * 创建掩码邮箱
	 * @param mail 原邮箱
	 * @return 掩码邮箱
	 */
	public final static String hidden_mail(String mail){
		int i = mail.indexOf('@');
		if(i==-1){
			return mail;
		}
		return mail.substring(0,2)+"******"+mail.substring(i, mail.length());
	}
}
